/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.retail;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import database.AuthenticationDAO;
import model.Customer;
import model.Memberaccount;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Thai Ha Cong
 */
@Namespace("/Member")
public class RegisterAction extends ActionSupport implements ModelDriven<Customer> {

    
    private Customer customer = new Customer();
    private String username;
    private String password;
    private String confirmPassword;

    public RegisterAction() {
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public String getUsername() {
        return username;
    }

    @RequiredStringValidator(message = "Username is required.")
    @StringLengthFieldValidator(message = "Username should contains 6-30 characters.", minLength = "6", maxLength = "30")
    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    @RequiredStringValidator(message = "Password is required.")
    @StringLengthFieldValidator(message = "Password should contains 6-30 characters.", minLength = "6", maxLength = "30")
    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    @FieldExpressionValidator(expression = "confirmPassword==password", message = "Passwords do not match.")
    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    @Override
    @Action(value = "register", results = {
        @Result(name = "success", location = "/member/login.jsp"),
        @Result(name = "input", location = "/member/register.jsp")
    })
    public String execute() throws Exception {
        String result;

        AuthenticationDAO adao = new AuthenticationDAO();

        Memberaccount memberaccount = new Memberaccount(username, password);
        customer.setMemeberaccount(memberaccount);

        if (adao.register(customer)) {
            addActionMessage("Register Successfully!");
            result = SUCCESS;
        } else {
            addActionError("Register Failed (due to existed username or email).");
            result = INPUT;
        }

        return result;
    }

    @Override
    @VisitorFieldValidator(message = "", appendPrefix = false)
    public Customer getModel() {
        return customer;
    }
}